<template>
    <!-- 终期台账表 -->
    <div>
        <div class="e-table" style="background-color: #fff">
            <!-- 表格 -->
            <div class="e-table">
                <!-- default-expand-all 默认展开全部 -->
                <el-table
                    :data="tableData"
                    ref="table"
                    border
                    style="width: 100%"
                    :header-cell-style="{ textAlign: 'center' }"
                    :cell-style="{ textAlign: 'center' }"
                    :tree-props="{
                        children: 'children',
                        hasChildren: 'hasChildren'
                    }"
                    lazy
                    row-key="uuid"
                    class="table"
                    default-expand-all
                    v-sortTable="{ 'tableData': tableData, '_this': this, 'ref': 'table' }"
                >
                    <el-table-column
                    prop="serialNumber" label="细目号" width="150" fixed align="center"
                    >
                       <template slot-scope="{ row }">
                                <span v-if="row.serialNumber">{{row.serialNumber+'.'+row.itemName}}</span>
                                <span v-else>{{row.itemName}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                    prop="itemOrExpenseName" label="细目或费用名称" min-width="150"
                    >
                    </el-table-column>
                    <el-table-column
                    prop="structureName" label="结构部位名称" min-width="150"
                    >
                    </el-table-column>
                    <el-table-column
                    prop="unit" label="单位" min-width="150"
                    >
                    </el-table-column>
                    <el-table-column align="center"  label="业主合同数量">
                    <el-table-column
                    prop="oldList" label="原清单" min-width="150"
                    >
                    </el-table-column>
                    <el-table-column
                    prop="addChange" label="累计变更" min-width="150"
                    >
                    </el-table-column>
                    <el-table-column
                    prop="afterChange" label="变更后" min-width="150"
                    >
                    </el-table-column>
                    </el-table-column>
                     <!-- 队伍 -->
                    <el-table-column align="center"  label="各协作队伍累计">
                    <el-table-column
                    prop="contractName" :label="item.contractName" min-width="150"
                    v-for="(item,index) in contractTable" :key='index'
                    >
                    <el-table-column
                    :prop="item.contractNumber" label="合同数量" min-width="150"
                    >
                    </el-table-column>
                    <el-table-column
                    :prop="item.contractPrice" label="合同单价" min-width="150"
                    >
                    </el-table-column>
                    <el-table-column
                    :prop="item.contractAmount" label="合同金额" min-width="150"
                    >
                    </el-table-column>
                    <el-table-column
                    :prop="item.settlementQuantity" label="结算数量" min-width="150"
                    >
                    </el-table-column>
                    <el-table-column
                    :prop="item.settlementAmount" label="结算金额" min-width="150"
                    >
                    </el-table-column>
                    </el-table-column>

                    <el-table-column
                    prop="total" label="合计" min-width="150"
                    >
                    <el-table-column
                    prop="settlementTotalQuantity" label="结算数量" min-width="150"
                    >
                    </el-table-column>
                    <el-table-column
                    prop="settlementTotalAmount" label="结算金额" min-width="150"
                    >
                    </el-table-column>
                    </el-table-column>
                    </el-table-column>

                    <el-table-column
                        prop="surplusProject"
                        label="剩余工程"
                        align="center"
                        min-width="150"
                    >
                    <el-table-column
                    prop="settlementSurplusQuantity" label="结算数量" min-width="150"
                    >
                    </el-table-column>
                    <el-table-column
                    prop="settlementSurplusAmount" label="结算金额" min-width="150"
                    >
                    </el-table-column>
                     </el-table-column>
                </el-table>
            </div>
        </div>
    </div>
</template>
<script>
// eslint-disable-next-line no-unused-vars
import { selectDataClient, showLoading, hideLoading, getUuid } from '@/utils/common'
export default {
    props: {
        billid: {
            type: String,
            default: ''
        }
    },
    data () {
        return {
            //合同列表
            contractTable: [],
            //记录合同名称方便后续使用
            contractNameList: [],
            //表格数据
            tableData: [],
            //合同工程内容
            projectContent: {
                serialNumber: 1,
                children: [], //子集数组
                itemName: '合同工程内容', //细目名称 扩大基础"
                type: 1, //结算清单类型(1:清单;2:加款;3:扣款;4:税金;5:暂扣款)
                uuid: getUuid(), //表格需要的id
            },
            //加款
            addMoney: {
                serialNumber: 2,
                children: [], //子集数组
                itemName: '加款', //细目名称 扩大基础"
                type: 2, //结算清单类型(1:清单;2:加款;3:扣款;4:税金;5:暂扣款)
                uuid: getUuid(), //表格需要的id
            },
            //扣款
            deductMoney: {
                serialNumber: 3,
                children: [], //子集数组
                itemName: '扣款', //细目名称 扩大基础"
                type: 3, //结算清单类型(1:清单;2:加款;3:扣款;4:税金;5:暂扣款)
                uuid: getUuid(), //表格需要的id
            },
            //税金
            taxes: {
                serialNumber: 4,
                children: [], //子集数组
                itemName: '税金', //细目名称 扩大基础"
                type: 4, //结算清单类型(1:清单;2:加款;3:扣款;4:税金;5:暂扣款)
                uuid: getUuid(), //表格需要的id
            },
            //暂扣款
            temporaryDeductMoney: {
                serialNumber: 5,
                children: [], //子集数组
                itemName: '暂扣款', //细目名称 扩大基础"
                type: 5, //结算清单类型(1:清单;2:加款;3:扣款;4:税金;5:暂扣款)
                uuid: getUuid(), //表格需要的id
            },
            //不含税结算价合计
            noTaxesTotalSettlement: {
                children: [], //子集数组
                itemName: '不含税结算价合计', //细目名称 扩大基础"
                type: null, //结算清单类型(1:清单;2:加款;3:扣款;4:税金;5:暂扣款)
                uuid: getUuid(), //表格需要的id
            },
            //含税结算价合计
            taxesTotalSettlement: {
                children: [], //子集数组
                itemName: '含税结算价合计', //细目名称 扩大基础"
                type: null, //结算清单类型(1:清单;2:加款;3:扣款;4:税金;5:暂扣款)
                uuid: getUuid(), //表格需要的id
            },
            //结算合计
            totalSettlement: {
                children: [], //子集数组
                itemName: '结算合计', //细目名称 扩大基础"
                type: null, //结算清单类型(1:清单;2:加款;3:扣款;4:税金;5:暂扣款)
                uuid: getUuid(), //表格需要的id
            },
        }
    },
    computed: {},
    watch: {},
    created () {},
    mounted () {
        // //设置父级默认值
        // this.setFatherParmes()
        // //设置获取合同数据
        // this.setContractTable()
        // //获取表格数据
        // this.setTemphttpData()
    },
    methods: {
        //为最父级设置默认参数
        setFatherParmes () {
            const obj = {
                oldList: 0,
                addChange: 0,
                afterChange: 0,
                settlementTotalQuantity: 0,
                settlementTotalAmount: 0,
                settlementSurplusQuantity: 0,
                settlementSurplusAmount: 0,
            }
            Object.assign(this.projectContent, obj)
            Object.assign(this.addMoney, obj)
            Object.assign(this.deductMoney, obj)
            Object.assign(this.taxes, obj)
            Object.assign(this.temporaryDeductMoney, obj)
            Object.assign(this.noTaxesTotalSettlement, obj)
            Object.assign(this.taxesTotalSettlement, obj)
            Object.assign(this.totalSettlement, obj)
        },
        //计算合计
        setTatolParmes (list) {
            const noTaxesTotalSettlement = list.filter(item=>[1, 2, 3].includes(item.type))
            const taxesTotalSettlement = list.filter(item=>[1, 2, 3, 4].includes(item.type))
            this.treeUpSummary(this.noTaxesTotalSettlement, noTaxesTotalSettlement)
            this.treeUpSummary(this.taxesTotalSettlement, taxesTotalSettlement)
            this.treeUpSummary(this.totalSettlement, list)
            this.tableData.push(this.noTaxesTotalSettlement, this.taxesTotalSettlement, this.totalSettlement)
        },
        //遍历树节点
        traversalTree (list) {
            list.forEach(out=>{
                if(out.children && out.children.length > 0) {
                    out.children.forEach(line=>{
                        if(line.children && line.children.length > 0) {
                            line.children.forEach(son=>{
                                if(son.children && son.children.length > 0) {
                                    this.treeUpSummary(son, son.children)
                                }
                            })
                            this.treeUpSummary(line, line.children)
                        }
                    })
                    this.treeUpSummary(out, out.children)
                }
            })
        },
        //统计某个树节点下所有子集的和
        treeUpSummary (item, list) {
            const noTarget =  ['serialNumber', 'itemOrExpenseName', 'itemName', 'itemNo', 'parentId', 'structureName', 'thisLevelId', 'type', 'unit', 'uuid', 'children']
            for (const key in item) {
                if(!noTarget.includes(key)) {
                    item[key] = list.reduce((a, b)=>a + b[key], 0)
                }
            }
            // const obj = {
            //     oldList: list.reduce((a, b)=>a + b.oldList, 0),
            //     addChange: list.reduce((a, b)=>a + b.addChange, 0),
            //     afterChange: list.reduce((a, b)=>a + b.afterChange, 0),
            //     settlementTotalQuantity: list.reduce((a, b)=>a + b.settlementTotalQuantity, 0),
            //     settlementTotalAmount: list.reduce((a, b)=>a + b.settlementTotalAmount, 0),
            //     settlementSurplusQuantity: list.reduce((a, b)=>a + b.settlementSurplusQuantity, 0),
            //     settlementSurplusAmount: list.reduce((a, b)=>a + b.settlementSurplusAmount, 0)
            // }
            // this.contractNameList.forEach(key=>{
            //     obj['contractNumber_' + key] = list.reduce((a, b)=>a + b['contractNumber_' + key], 0)
            //     obj['contractPrice_' + key] = list.reduce((a, b)=>a + b['contractPrice_' + key], 0)
            //     obj['contractAmount_' + key] = list.reduce((a, b)=>a + b['contractAmount_' + key], 0)
            //     obj['settlementQuantity_' + key] = list.reduce((a, b)=>a + b['settlementQuantity_' + key], 0)
            //     obj['settlementAmount_' + key] =  list.reduce((a, b)=>a + b['settlementAmount_' + key], 0)
            // })
            // return obj
        },
        //创建指定合同名称下的参数
        setContractParmes (contract, item) {
            const contractName = contract.contractName
            const obj = {}
            obj['contractNumber_' + contractName] = Math.floor(Math.random() * 20 + 1)
            obj['contractPrice_' + contractName] = Math.floor(Math.random() * 1000 + 100)
            obj['contractAmount_' + contractName] = Math.floor(Math.random() * 1000 + 100)
            obj['settlementQuantity_' + contractName] = Math.floor(Math.random() * 20 + 1)
            obj['settlementAmount_' + contractName] = Math.floor(Math.random() * 1000 + 100)
            obj['settlementTotalAmount'] =  item['settlementTotalAmount'] ? obj['settlementAmount_' + contractName] + item['settlementTotalAmount'] : obj['settlementAmount_' + contractName]
            obj['settlementTotalQuantity'] = item['settlementTotalQuantity'] ? obj['settlementQuantity_' + contractName] + item['settlementTotalQuantity'] :
                obj['settlementQuantity_' + contractName]
            Object.assign(item, obj)
        },
        //创建指定合同列表
        setContractTable () {
            const c1 = this.setTempContract('ty清单测试合同')
            const c2 = this.setTempContract('材料签署正式合同')
            this.contractNameList.push('ty清单测试合同', '材料签署正式合同')
            this.contractTable.push(c1, c2)
        },
        //模拟合同
        setTempContract (contractName) {
            const obj = {
                contractName,
                contractNumber: 'contractNumber_' + contractName,
                contractPrice: 'contractPrice_' + contractName,
                contractAmount: 'contractAmount_' + contractName,
                settlementQuantity: 'settlementQuantity_' + contractName,
                settlementAmount: 'settlementAmount_' + contractName
            }
            return obj
        },
        //将数据转为表格需要的树
        listConversionTreeOther (arr, parentId) {
            const tree = []
            let temp
            for (let i = 0; i < arr.length; i++) {
                if (arr[i].parentId == parentId) {
                    arr[i].uuid = getUuid()
                    const obj = arr[i]
                    temp = this.listConversionTreeOther(arr, arr[i].thisLevelId)
                    if (temp.length > 0) {
                        obj.children = temp
                    } else {
                        obj.children = []
                        obj.lowestChild = true
                    }
                    tree.push(obj)
                }
            }
            return tree
        },
        //加扣款
        setAddOrDeductMoney (type) {
            const str = type == 2 ? '加' : '扣'
            const thisId = type == 2 ? 'Add' : 'Deduct'
            const thisIdSon = type == 2 ? '7979' : '8282'
            const addMoney = [
                {
                    itemNo: '', itemName: `工程${str}款`, structureName: '', unit: '', oldList: 0, addChange: 0, afterChange: 0, thisLevelId: `g1${thisId}`, parentId: '0', type, settlementSurplusQuantity: 0, settlementSurplusAmount: 0
                }, {
                    itemNo: '', itemName: `材料${str}款`, structureName: '', unit: '', oldList: 0, addChange: 0, afterChange: 0, thisLevelId: `c1${thisId}`, parentId: '0', type, settlementSurplusQuantity: 0, settlementSurplusAmount: 0
                }, {
                    itemNo: '', itemName: `周材${str}款`, structureName: '', unit: '', oldList: 0, addChange: 0, afterChange: 0, thisLevelId: `z1${thisId}`, parentId: '0', type, settlementSurplusQuantity: 0, settlementSurplusAmount: 0
                },
                {
                    itemNo: '', itemName: `设备${str}款`, structureName: '', unit: '', oldList: 0, addChange: 0, afterChange: 0, thisLevelId: `s1${thisId}`, parentId: '0', type, settlementSurplusQuantity: 0, settlementSurplusAmount: 0
                },
                {
                    itemNo: '', itemName: '奖金', structureName: '', unit: '', oldList: 0, addChange: 0, afterChange: 0, thisLevelId: `12124578${thisIdSon}`, parentId: `g1${thisId}`, type, settlementSurplusQuantity: 10, settlementSurplusAmount: 1025
                },
                {
                    itemNo: '', itemName: '结算调整', structureName: '', unit: '', oldList: 0, addChange: 0, afterChange: 0, thisLevelId: `154781${thisIdSon}`, parentId: `c1${thisId}`, type, settlementSurplusQuantity: 11, settlementSurplusAmount: 1048
                },
                {
                    itemNo: '', itemName: '保险费', structureName: '', unit: '', oldList: 0, addChange: 0, afterChange: 0, thisLevelId: `965632${thisIdSon}`, parentId: `z1${thisId}`, type, settlementSurplusQuantity: 12,
                    settlementSurplusAmount: 1079
                },
                {
                    itemNo: '', itemName: '试验检验表', structureName: '', unit: '', oldList: 0, addChange: 0, afterChange: 0, thisLevelId: `322147${thisIdSon}`, parentId: `s1${thisId}`, type, settlementSurplusQuantity: 13, settlementSurplusAmount: 1099
                }
            ]
            return addMoney
        },
        //模拟接口返回数据格式
        setTemphttpData () {
            const list = [
                //1合同工程内容
                {
                    itemNo: '', itemName: '200章', structureName: '', unit: '', oldList: 50, addChange: 10, afterChange: 60, thisLevelId: '1', parentId: '0', type: 1, settlementSurplusQuantity: 4, settlementSurplusAmount: 1478
                }, {
                    itemNo: '', itemName: '200-1', structureName: '', unit: '', oldList: 50, addChange: 10, afterChange: 60, thisLevelId: '1-1', parentId: '1', type: 1, settlementSurplusQuantity: 4, settlementSurplusAmount: 1478
                }, {
                    itemNo: '', itemName: '200-1-a', structureName: '', unit: '吨', oldList: 50, addChange: 10, afterChange: 60, thisLevelId: '1-1-1', parentId: '1-1', type: 1, settlementSurplusQuantity: 4, settlementSurplusAmount: 1478
                },
                {
                    itemNo: '', itemName: '200-1-b', structureName: '', unit: '吨', oldList: 50, addChange: 10, afterChange: 60, thisLevelId: '1-1-2', parentId: '1-1', type: 1, settlementSurplusQuantity: 9, settlementSurplusAmount: 1698
                },
                {
                    itemNo: '', itemName: '200-2', structureName: '', unit: '', oldList: 50, addChange: 10, afterChange: 60, thisLevelId: '1-2', parentId: '1', type: 1, settlementSurplusQuantity: 4, settlementSurplusAmount: 1478
                }, {
                    itemNo: '', itemName: '200-2-a', structureName: '', unit: '吨', oldList: 50, addChange: 10, afterChange: 60, thisLevelId: '1-2-1', parentId: '1-2', type: 1, settlementSurplusQuantity: 15, settlementSurplusAmount: 3214
                },
                {
                    itemNo: '', itemName: '200-2-b', structureName: '', unit: '吨', oldList: 50, addChange: 10, afterChange: 60, thisLevelId: '1-2-2', parentId: '1-2', type: 1, settlementSurplusQuantity: 19, settlementSurplusAmount: 2258
                },
                //2加款
                ...this.setAddOrDeductMoney(2),
                //3扣款
                ...this.setAddOrDeductMoney(3),
                //4税金
                {
                    itemNo: '', itemName: '', structureName: '', unit: '', oldList: 0, addChange: 0, afterChange: 0, thisLevelId: 'taxe', parentId: '0', type: 4, settlementSurplusQuantity: 0, settlementSurplusAmount: 0
                },
                //5暂扣款
                {
                    itemNo: '', itemName: '质量保证金', structureName: '', unit: '', oldList: 0, addChange: 0, afterChange: 0, thisLevelId: 'bao1', parentId: '0', type: 5, settlementSurplusQuantity: 2, settlementSurplusAmount: 851
                },
                {
                    itemNo: '', itemName: '民工工资保证金', structureName: '', unit: '', oldList: 0, addChange: 0, afterChange: 0, thisLevelId: 'bao2', parentId: '0', type: 5, settlementSurplusQuantity: 3, settlementSurplusAmount: 998
                },
            ]
            list.forEach(data=>{
                this.contractTable.forEach(contract=>{
                    this.setContractParmes(contract, data)
                })
            })
            //为父级设置合同参数
            this.contractTable.forEach(contract=>{
                this.setContractParmes(contract, this.projectContent)
                this.setContractParmes(contract, this.addMoney)
                this.setContractParmes(contract, this.deductMoney)
                this.setContractParmes(contract, this.taxes)
                this.setContractParmes(contract, this.temporaryDeductMoney)
                this.setContractParmes(contract, this.noTaxesTotalSettlement)
                this.setContractParmes(contract, this.taxesTotalSettlement)
                this.setContractParmes(contract, this.totalSettlement)
            })
            const contractList = list.filter(item=>item.type == 1)//合同清单
            const addMoneyList = list.filter(item=>item.type == 2)//加款
            const deductMoneyList = list.filter(item=>item.type == 3)//扣款
            const taxesList = list.filter(item=>item.type == 4)//税金
            const zankouMoneyList = list.filter(item=>item.type == 5)//暂扣款
            this.projectContent.children = this.listConversionTreeOther(contractList, '0')
            this.addMoney.children = this.listConversionTreeOther(addMoneyList, '0')
            this.deductMoney.children = this.listConversionTreeOther(deductMoneyList, '0')
            this.taxes.children = this.listConversionTreeOther(taxesList, '0')
            this.temporaryDeductMoney.children = this.listConversionTreeOther(zankouMoneyList, '0')
            this.tableData.push(this.projectContent, this.addMoney, this.deductMoney, this.taxes, this.temporaryDeductMoney)
            //将所有参数汇总到父级不包括合计
            this.traversalTree(this.tableData)
            //设置合计
            this.setTatolParmes(this.tableData)
        },
    }
}
</script>
<style lang="scss" scoped>
::v-deep .hide-icon {
    .el-table__expand-icon {
        display: none;
    }
}
::v-deep.el-table__row {
    .cell {
        display: flex;
        align-items: center;
    }
}
::v-deep .el-popover__reference-wrapper {
    display: flex;
    height: 100%;
}
.select_box {
    display: flex;
    align-items: center;
    ::v-deep .el-radio-group {
        display: flex;
        .el-radio {
            display: flex;
            align-items: center;
            margin-right: 10px;
        }
    }
}
</style>
